package com.concurrent.chapter5.concurrent01;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyCallable implements Callable<String> {
	private int age;
	
	public MyCallable(int age) {
		super();
		this.age = age;
	}
	@Override
	public String call() throws Exception {
		TimeUnit.SECONDS.sleep(8);
		return "返回值 年龄是：" + age;
	}
	
	public static void main(String[] args) {
		MyCallable myCallable = new MyCallable(22);
		int corePoolSize = 2;
		int maximumPoolSize = 3;
		int keepAliveTime = 5;
		TimeUnit unit = TimeUnit.SECONDS;
		LinkedBlockingDeque<Runnable> workQueue = new LinkedBlockingDeque<Runnable>();
		ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
		Future<String> future = threadPoolExecutor.submit(myCallable);
		try {
			System.out.println(System.currentTimeMillis());
			String string = future.get();//阻塞
			System.out.println(string);
			System.out.println(System.currentTimeMillis());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
